package grex.genes;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import grex.BNFException;
import grex.ErrorManager;
import grex.Prediction;
import grex.PredictionContainer;

import java.text.NumberFormat;
public class LocalMean extends Gene {
  private static final int NROFCILDREN=0;
  private double mean =-1;
  private NumberFormat nf = NumberFormat.getInstance();
  public LocalMean() throws BNFException{
    super(NROFCILDREN);
     nf.setMaximumFractionDigits(2);

  }

  public synchronized void execute(Gene parent,PredictionContainer data,int mode) throws GeneException {
      this.parent = parent;         
      if(mode == Gene.TRAIN_MODE){ 
          mean=0;
          for (Prediction p : data.values()) {
            mean+=p.getTargetValue();
          }
          mean=mean/data.values().size();
      }
      for (Prediction p : data.values()) {
            p.setPrediction(mean);
        }
  }
  
  public void setMean(double mean){
      this.mean=mean;
  }

 public Object clone(){ ;
    LocalMean newLocalMean;
    try{
      newLocalMean = new LocalMean();
          newLocalMean.setMean(mean);
          newLocalMean.setOptions(ops);
          newLocalMean.setEnvironment(environment);
          newLocalMean.setParent(parent);
    }catch(grex.BNFException e){
      ErrorManager.getInstance().reportError(e);
      return null;
    }
    return newLocalMean;
  }
  /** @todo Om endast enn kategori finns kommer denna att volja lika ofta somma alla andra sammtidigt */

  public String toString(){                     
            return "LM:" + nf.format(mean)+"";
  }
}

